package application;

import java.net.URL;
import java.text.DateFormatSymbols;
import java.util.Arrays;
import java.util.Locale;
import java.util.ResourceBundle;

import base.Manager;
import base.Person;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;

import javafx.scene.chart.CategoryAxis;

import javafx.scene.chart.BarChart;

import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;

public class AttendanceChart2Controller implements Initializable{
	@FXML
	private Button bcheckattendance;
	@FXML
	private Button bstaffinfo;
	@FXML
	private Button battendance_st;
	@FXML
	private Button bdaily_ass;
	@FXML
	private Button bbulletin;
	@FXML
	private Button blogout;
	@FXML
	private Button workday;
	
	@FXML
	private BarChart<String, Integer> barChart;
	@FXML
	private CategoryAxis xAxis;
	@FXML
	private NumberAxis yAxis;
	
	private Main application;
	private int k=0;
	
	Person p = new Manager();
	
	private ObservableList<String> monthNames = FXCollections.observableArrayList();

	@Override
	public void initialize(URL location, ResourceBundle resources) {
		// TODO Auto-generated method stub
		
	} 
	public void setApp(Main application){
		this.application=application;
	}
	public void setPersonData(String id){
		String[] months = DateFormatSymbols.getInstance(Locale.ENGLISH).getShortMonths();
        // Convert it to a list and add it to our ObservableList of months.
        monthNames.addAll(Arrays.asList(months));

        k = Integer.parseInt(id);
        if(k==p.getID()) k=p.getID();
        // Assign the month names as categories for the horizontal axis.
        xAxis.setCategories(monthNames);
        int[] monthdata = new int[12];
        for(int i=1;i<=12;i++){
        	String m=String.valueOf(i);
        	monthdata[i-1]=db.DB_tools.getoffdayinfo(m, k);
        }
        XYChart.Series<String, Integer> series = new XYChart.Series<>();

        // Create a XYChart.Data object for each month. Add it to the series.
        for (int i = 0; i < monthdata.length; i++) {
            series.getData().add(new XYChart.Data<>(monthNames.get(i), monthdata[i]));
        }
        
        barChart.getData().add(series);
	}
	
	public void gotoAttendanceChart(){
		String s = String.valueOf(k);
		application.gotoworkdayinfo(p,s);
	}
	
	
	@FXML
	public void bbulletin(ActionEvent event){
		if("Manager".equals(p.getLevel())){
		    Manager m = (Manager)p;
			application.gotoaddBulletin(m);
		}
		else{
			application.gotoBulletin(p);
		}
		
	}
	
	@FXML
	public void gotoAssignment(ActionEvent event)
	{
		if("Manager".equals(p.getLevel()))
		{
			Manager m=(Manager)p;
			application.gotoaddAssignment(m);
		}
		else{
			application.gotoAssignment(p);
		}
	}
	
	@FXML
	public void bcheckattendance(ActionEvent event)
	{
		application.gotoCheckAttendance(p);	
	}
	

	@FXML
	public void bstaffinfo(ActionEvent event)
	{
		if("Manager".equals(p.getLevel()))
		{
			Manager m=(Manager)p;
			System.out.println("I'm Manager");
			application.gotoNamelist(m);
		}
		else
		{
			application.gotoStaffinfo(p);
		}
	}
	
	@FXML
	public void bdaily_ass(ActionEvent event)
	{
		if("Manager".equals(p.getLevel())){
		    Manager m = (Manager)p;
			application.gotoaddAssignment(m);
		}
		else{
			application.gotoAssignment(p);
		}
	}
	
	@FXML
	public void battendance_st(ActionEvent event){
		if("Manager".equals(p.getLevel())){
		    Manager m = (Manager)p;
			application.gotonamelist(m);
		}
		else{
			String s = String.valueOf(p.getID());
			application.gotoworkdayinfo(p, s);
		}
	}
	
	


}